use std::collections::HashMap;

pub fn convert_base(num_str: &str, to_base: u32) -> String {
    // 分割得到 val, base
    let num_str = &num_str[..num_str.len() - 1];
    let spilted: Vec<&str> = num_str.split('(').collect();
    let (val, base) = (spilted[0], spilted[1]);
    
    // 将 val 转换为 u32 值 from_val
    let val: String = val.chars().rev().collect();
    let radix = base.parse::<u32>().unwrap();
    let mut from_val = 0;
    for (i, x) in val.chars().enumerate() {
        let x = x.to_digit(radix).unwrap();
        from_val += x * radix.pow(i as u32);
    }

    // 将 from_val 转换为目标字符串 to_val
    let hex_map: HashMap<u32, &str> = vec![
        (0u32, "0"),
        (1u32, "1"),
        (2u32, "2"),
        (3u32, "3"),
        (4u32, "4"),
        (5u32, "5"),
        (6u32, "6"),
        (7u32, "7"),
        (8u32, "8"),
        (9u32, "9"),
        (10u32, "a"),
        (11u32, "b"),
        (12u32, "c"),
        (13u32, "d"),
        (14u32, "e"),
        (15u32, "f"),
    ].into_iter().collect();
    if from_val == 0 {
        return "0".to_string();
    }
    let mut to_val = String::new();
    while from_val > 0 {
        let remain = from_val % to_base;
        to_val.push_str(hex_map[&remain]);
        from_val /= to_base;
    }

    to_val.chars().rev().collect()
}
